/****************************************************************************
 *
 *   Copyright (c) 2019 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#pragma once

#include "work_item.hpp"
#include <px4_platform_common/px4_sem.h>

namespace px4
{

/**
 * @class WorkItemSingleShot
 * @brief Run a method on a specific work queue and wait for it to exit
 *
 * WorkItemSingleShot is a one-time work item that executes a user-provided
 * method on a specific work queue and provides a wait() method to block
 * until the method completes.
 *
 * This is commonly used for driver initialization where you need to run
 * initialization code on a specific work queue (e.g., the SPI bus work queue)
 * and wait for it to complete before proceeding.
 *
 * Example usage:
 * @code
 *   void init_sensor(void *arg) {
 *       // Initialize sensor on SPI bus work queue
 *       MySensor *sensor = (MySensor *)arg;
 *       sensor->initialize();
 *   }
 *
 *   MySensor sensor;
 *   WorkItemSingleShot initializer(px4::wq_configurations::SPI2, init_sensor, &sensor);
 *   initializer.ScheduleNow();
 *   initializer.wait();  // Block until init_sensor() completes
 * @endcode
 */
class WorkItemSingleShot : public px4::WorkItem
{
public:
	/**
	 * @brief Worker method signature
	 *
	 * The user-provided method that will be executed on the work queue.
	 * @param arg User-provided argument passed to the method
	 */
	using worker_method = void (*)(void *arg);

	/**
	 * @brief Constructor with work queue configuration
	 *
	 * @param config Work queue configuration (e.g., px4::wq_configurations::SPI2)
	 * @param method User method to execute
	 * @param argument User argument to pass to the method
	 */
	WorkItemSingleShot(const px4::wq_config_t &config, worker_method method, void *argument);

	/**
	 * @brief Constructor with existing WorkItem
	 *
	 * Creates a single-shot work item that runs on the same work queue as
	 * the provided WorkItem.
	 *
	 * @param work_item Existing work item (to copy work queue from)
	 * @param method User method to execute
	 * @param argument User argument to pass to the method
	 */
	WorkItemSingleShot(const px4::WorkItem &work_item, worker_method method, void *argument);

	/**
	 * @brief Destructor
	 */
	~WorkItemSingleShot();

	// Delete default constructor
	WorkItemSingleShot() = delete;

	// Delete copy, assignment, move, move assignment
	WorkItemSingleShot(const WorkItemSingleShot &) = delete;
	WorkItemSingleShot &operator=(const WorkItemSingleShot &) = delete;
	WorkItemSingleShot(WorkItemSingleShot &&) = delete;
	WorkItemSingleShot &operator=(WorkItemSingleShot &&) = delete;

	/**
	 * @brief Wait for the work item to complete
	 *
	 * Blocks the calling task until the worker method completes execution.
	 * This uses a semaphore internally to synchronize.
	 */
	void wait();

protected:
	/**
	 * @brief Run method (called by work queue)
	 *
	 * Executes the user-provided method and signals the semaphore.
	 */
	void Run() override;

private:
	void *_argument;           ///< User argument to pass to the method
	worker_method _method;     ///< User method to execute
	px4_sem_t _sem;            ///< Semaphore for synchronization
};

} // namespace px4

